package gtkwire.widget;

import gtkwire.*;
import gtkwire.gobject.*;

/**
*Base class for widgets which visualize an adjustment.
*/
public abstract class GtkRange extends GtkWidget
{
	private GtkAdjustment adjustment = null;

	protected GtkRange( GtkAdjustment adjustment )
	{
		super();
		this.adjustment = adjustment;
	}

	protected GtkRange( String name, GladeKey key )
	{
		super( name, key );
	}

	protected String[] getCreateData()
	{ 
		String[] msg = { Integer.toString( adjustment.getId() ) };
		return msg;
	}

	/**
	*Used to get current value of range. 
	*/
	public double getValue()
	{
		GTKWireMessage answer = app().sendCommandRequest( name, widgetType, GTKWireCommandRequest.GET_VALUE, null );
		return ((Double) answer.getMessageObject()).doubleValue();
	}


	public void setAdjustment( GtkAdjustment adjusment )
	{
		this.adjustment = adjustment;
		app().sendCommand( GObject.MAGIC_NAME, GTKWireCommand.SET_ADJUSTMENT, adjustment.getId() );
	}

	/**
	*Sets the step and page sizes for the range. The step size is used when the user clicks the GtkScrollbar 
	arrows or moves GtkScale via arrow keys. The page size is used for example when moving via Page Up or Page Down keys.
	*/
	public void setIncrements( double step, double page )
	{
		double[] msg = new double[2];
		msg[ 0 ] = step;
		msg[ 1 ] = page;
		app().sendCommand( name, GTKWireCommand.SET_INCREMENTS, msg );
	}

	/**
	*Sets the current value of the range. If the value is outside the minimum or maximum range values, 
	*it will be clamped to fit inside them. The range emits the "value-changed" signal if the value changes.
	*/
	public void setValue( double val )
	{
		app().sendCommand( name, GTKWireCommand.SET_VALUE, val );
	}
	//--- works but badly. Setting range by just calling gtk_range_set_range after is not good enough.
	//--- might not be meant to call after init

	/*
	*This method does not work currently.

	public void setRange( double min, double max )
	{
		if( min > max )
		{
			double tmp = max;
			max = min;
			min = tmp;
		}
		double[] data = new double[ 2 ];
		data[ 0 ] = min;
		data[ 1 ] = max;
		app().sendCommand( name, GTKWireCommand.SET_RANGE, data );
	}
	*/

	/**
	*Sets the update policy for the range.
	*/
	public void setUpdatePolicy( int policy )
	{
		app().sendCommand( name, GTKWireCommand.SET_UPDATE_POLICY, policy );
	}

}//end class